<?php
namespace app\admin\controller;
class Admin extends Common
{
    public function lst(){
        $adminRes=db('admin')->alias('a')->field('a.*,b.title')->join('auth_group b','a.group_id=b.id')->where('group_id','1')->paginate(10);
        $count=db('admin')->where('group_id','1')->count();
        $this->assign([
            'adminRes'=>$adminRes,
            'count'=>$count,
        ]);
        return view();
    }

    public function add()
    {
    	if(request()->isAjax()){
    		$data=input('post.')['data'];
            $groupMembers = input('post.')['group_members'];
    		$data['salt']=mt_rand(1000,9999);
            $data['password']=md5(md5($data['password']).$data['salt']);
            $data['create_time']=time();
            $data['join_time'] = strtotime($data['join_time']);
            //添加数据到admin表，并返回id
            if($data['group_id'] == 5 && $groupMembers)
            {
                $groupMembers = json_decode($groupMembers,true);
                $arr = [];
                foreach ($groupMembers as $k => $v) {
                    $arr[] = $v['value'];
                }
                $data['group_members'] = implode(',', $arr);
            }

            $add=db('admin')->insertGetId($data);
            //对应管理员和用户组id
            $_data=array();
            $_data['uid']=$add;
            $_data['group_id']=$data['group_id'];
            //添加用户组明细表信息
            $addGroupAccess=db('authGroupAccess')->insert($_data);
            if($add && $addGroupAccess){
                return ['msg'=>'新增管理员成功','icon'=>1];
            }else{
                return ['msg'=>'新增管理员失败','icon'=>2];
            }
            return;
    	}
        $groupRes=db('authGroup')->field('id,title,status')->select();
        $this->assign([
            'groupRes'=>$groupRes,
        ]);
        return view();
    }

    public function edit()
    {
        if(request()->isAjax()){
            $data=input('post.')['data'];
            $groupMembers = input('post.')['group_members'];
            if($data['password'] !== '' || $data['repass'] !== ''){
                if($data['password'] !== $data['repass']){
                    return ['msg'=>'两次密码输入不一致','icon'=>5];
                }
            }
            $data['join_time'] = strtotime($data['join_time']);
            unset($data['repass']);
            //查询密钥
            $userInfo=db('admin')->where('id',$data['id'])->find();
            $salt=$userInfo['salt'];
            if($data['password']){
                $data['password']=md5(md5($data['password']).$salt);
            }else{
                unset($data['password']);
            }
            if( (session('group_id','','admin') == 5 || $data['group_id'] == 5) && $groupMembers)
            {
                $groupMembers = json_decode($groupMembers,true);
                $arr = [];
                foreach ($groupMembers as $k => $v) {
                    $arr[] = $v['value'];
                }
                $data['group_members'] = implode(',', $arr);
            }
            if($data['group_id'] != 5 && session('group_id','','admin') != 5)
                $data['group_members'] = '';
            $save=db('admin')->update($data);
            //添加auth权限 用户组明细表内容
            if($data['group_id'])
                db('authGroupAccess')->where(array('uid'=>$data['id']))->update(['group_id'=>$data['group_id']]);
            if($save !== false){
                return ['msg'=>'编辑管理员成功','icon'=>1];
            }else{
                return ['msg'=>'编辑管理员失败','icon'=>2];
            }
            return;
        }
        $groupRes=db('authGroup')->select();
    	$admins=db('admin')->find(input('id'));
    	$this->assign([
    		'admins'=>$admins,
            'groupRes'=>$groupRes,
    	]);
        return view();
    }

    public function del()
    {
        if(request()->isAjax()){
            $id=input('id');
        	if($id == 1){
        		return json(['msg'=>'超级管理员不允许删除','icon'=>2]);
        	}else{
                //删除auth权限用户组明细表内容
                db('authGroupAccess')->where(array('uid'=>$id))->delete();
        		$del=db('admin')->delete($id);
        		if($del){
                    return ['msg'=>'删除管理员成功','icon'=>1];
        		}else{
        			return ['msg'=>'删除管理员失败','icon'=>2];
        		}
        	}
        }
    }
    //批量删除
    public function delAll()
    {
        if(request()->isAjax()){
            $data=input('post.')['data'];
            $data= array_filter($data);
            foreach ($data as $k => $v){
                //删除auth权限用户组明细表内容
                db('authGroupAccess')->where(array('uid'=>$v))->delete();
                $del=db('admin')->delete($v);
            }
            return ['msg'=>'批量删除管理员成功','icon'=>1];
        } 
    }
    
    //ajax更新开启状态
    public function changeStatus()
    {
        if(request()->isAjax()){
            $id=input('id');
            $status=input('status');
            if($status == 1){
                db('admin')->where(['id'=>$id])->update(['status'=>0]);
                return ['msg'=>'已禁用','icon'=>2];
            }else{
                db('admin')->where(['id'=>$id])->update(['status'=>1]);
                return ['msg'=>'已启用','icon'=>1];
            }
        }else{
            $this->error("非法操作！");
        }
    }


    

}



